home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / TicTacToe / TicTacToe.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-10-12  |  4.0 KB  |  206 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8.  
  9. public class TicTacToe extends Applet {
  10.    int white;
  11.    int black;
  12.    static final int[] moves = new int[]{4, 0, 2, 6, 8, 1, 3, 5, 7};
  13.    static boolean[] won = (boolean[])(new byte[512]);
  14.    static final int DONE = 511;
  15.    // $FF: renamed from: OK int
  16.    static final int field_0 = 0;
  17.    static final int WIN = 1;
  18.    static final int LOSE = 2;
  19.    static final int STALEMATE = 3;
  20.    boolean first = true;
  21.    Image notImage;
  22.    Image crossImage;
  23.  
  24.    static void isWon(int pos) {
  25.       for(int i = 0; i < 511; ++i) {
  26.          if ((i & pos) == pos) {
  27.             won[i] = true;
  28.          }
  29.       }
  30.  
  31.    }
  32.  
  33.    int bestMove(int white, int black) {
  34.       int bestmove = -1;
  35.  
  36.       label57:
  37.       for(int i = 0; i < 9; ++i) {
  38.          int mw = moves[i];
  39.          if ((white & 1 << mw) == 0 && (black & 1 << mw) == 0) {
  40.             int pw = white | 1 << mw;
  41.             if (won[pw]) {
  42.                return mw;
  43.             }
  44.  
  45.             for(int mb = 0; mb < 9; ++mb) {
  46.                if ((pw & 1 << mb) == 0 && (black & 1 << mb) == 0) {
  47.                   int pb = black | 1 << mb;
  48.                   if (won[pb]) {
  49.                      continue label57;
  50.                   }
  51.                }
  52.             }
  53.  
  54.             if (bestmove == -1) {
  55.                bestmove = mw;
  56.             }
  57.          }
  58.       }
  59.  
  60.       if (bestmove != -1) {
  61.          return bestmove;
  62.       } else {
  63.          for(int i = 0; i < 9; ++i) {
  64.             int mw = moves[i];
  65.             if ((white & 1 << mw) == 0 && (black & 1 << mw) == 0) {
  66.                return mw;
  67.             }
  68.          }
  69.  
  70.          return -1;
  71.       }
  72.    }
  73.  
  74.    boolean yourMove(int m) {
  75.       if (m >= 0 && m <= 8) {
  76.          if (((this.black | this.white) & 1 << m) != 0) {
  77.             return false;
  78.          } else {
  79.             this.black |= 1 << m;
  80.             return true;
  81.          }
  82.       } else {
  83.          return false;
  84.       }
  85.    }
  86.  
  87.    boolean myMove() {
  88.       if ((this.black | this.white) == 511) {
  89.          return false;
  90.       } else {
  91.          int best = this.bestMove(this.white, this.black);
  92.          this.white |= 1 << best;
  93.          return true;
  94.       }
  95.    }
  96.  
  97.    int status() {
  98.       if (won[this.white]) {
  99.          return 1;
  100.       } else if (won[this.black]) {
  101.          return 2;
  102.       } else {
  103.          return (this.black | this.white) == 511 ? 3 : 0;
  104.       }
  105.    }
  106.  
  107.    public void init() {
  108.       this.notImage = ((Applet)this).getImage(((Applet)this).getCodeBase(), "images/not.gif");
  109.       this.crossImage = ((Applet)this).getImage(((Applet)this).getCodeBase(), "images/cross.gif");
  110.    }
  111.  
  112.    public void paint(Graphics g) {
  113.       Dimension d = ((Component)this).size();
  114.       g.setColor(Color.black);
  115.       int xoff = d.width / 3;
  116.       int yoff = d.height / 3;
  117.       g.drawLine(xoff, 0, xoff, d.height);
  118.       g.drawLine(2 * xoff, 0, 2 * xoff, d.height);
  119.       g.drawLine(0, yoff, d.width, yoff);
  120.       g.drawLine(0, 2 * yoff, d.width, 2 * yoff);
  121.       int i = 0;
  122.  
  123.       for(int r = 0; r < 3; ++r) {
  124.          for(int c = 0; c < 3; ++i) {
  125.             if ((this.white & 1 << i) != 0) {
  126.                g.drawImage(this.notImage, c * xoff + 1, r * yoff + 1, this);
  127.             } else if ((this.black & 1 << i) != 0) {
  128.                g.drawImage(this.crossImage, c * xoff + 1, r * yoff + 1, this);
  129.             }
  130.  
  131.             ++c;
  132.          }
  133.       }
  134.  
  135.    }
  136.  
  137.    public boolean mouseUp(Event evt, int x, int y) {
  138.       switch (this.status()) {
  139.          case 1:
  140.          case 2:
  141.          case 3:
  142.             ((Applet)this).play(((Applet)this).getCodeBase(), "audio/return.au");
  143.             this.white = this.black = 0;
  144.             if (this.first) {
  145.                this.white |= 1 << (int)(Math.random() * (double)9.0F);
  146.             }
  147.  
  148.             this.first = !this.first;
  149.             ((Component)this).repaint();
  150.             return true;
  151.          default:
  152.             Dimension d = ((Component)this).size();
  153.             int c = x * 3 / d.width;
  154.             int r = y * 3 / d.height;
  155.             if (this.yourMove(c + r * 3)) {
  156.                ((Component)this).repaint();
  157.                switch (this.status()) {
  158.                   case 1:
  159.                      ((Applet)this).play(((Applet)this).getCodeBase(), "audio/joy.au");
  160.                      break;
  161.                   case 2:
  162.                      ((Applet)this).play(((Applet)this).getCodeBase(), "audio/joy.au");
  163.                   case 3:
  164.                      break;
  165.                   default:
  166.                      if (this.myMove()) {
  167.                         ((Component)this).repaint();
  168.                         switch (this.status()) {
  169.                            case 1:
  170.                               ((Applet)this).play(((Applet)this).getCodeBase(), "audio/joy.au");
  171.                               break;
  172.                            case 2:
  173.                               ((Applet)this).play(((Applet)this).getCodeBase(), "audio/joy.au");
  174.                            case 3:
  175.                               break;
  176.                            default:
  177.                               ((Applet)this).play(((Applet)this).getCodeBase(), "audio/ding.au");
  178.                         }
  179.                      } else {
  180.                         ((Applet)this).play(((Applet)this).getCodeBase(), "audio/beep.au");
  181.                      }
  182.                }
  183.             } else {
  184.                ((Applet)this).play(((Applet)this).getCodeBase(), "audio/beep.au");
  185.             }
  186.  
  187.             return true;
  188.       }
  189.    }
  190.  
  191.    public String getAppletInfo() {
  192.       return "TicTacToe by Arthur van Hoff";
  193.    }
  194.  
  195.    static {
  196.       isWon(7);
  197.       isWon(56);
  198.       isWon(448);
  199.       isWon(73);
  200.       isWon(146);
  201.       isWon(292);
  202.       isWon(273);
  203.       isWon(84);
  204.    }
  205. }
  206.